package com.qiedan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiedan.bean.User;
import com.qiedan.service.UserService;
import com.qiedan.mapper.UserMapper;

import com.qiedan.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    /**
     * 用户注册
     * @param user
     */
    @Override
    public R<Object> register(User user) {
        R<Object> resp = new R<Object>();
        //判断前端传来的数据是否为null                    ||!"".equals(user.getUsername())||!"".equals(user.getPassword())||user.getUsername()==null||user.getPassword()==null
        if(ObjectUtils.isEmpty(user)){
            resp.setMsg("数据接收异常!");
            return resp;
        }
        String username = user.getUsername();
        User user1 = selectUserByUsername(username);
        if (user1!=null) {
            resp.setMsg("用户名："+username+"已经被注册!");

            return resp;
        }
        //恩能够到这说明该用户名可用，需要将该用户信息插入到数据
        Date nowTime = new Date();
        user.setCreatetime(nowTime);
        nowTime = new Date();
        user.setUpdatetime(nowTime);
        user.setType(1);
        baseMapper.insert(user);
        resp.setMsg("您已经注册成功，快去登陆吧!");
        resp.setCode(1);
        return resp;
    }

    /**
     * 用户登陆
     * @param user
     * @return
     */
    @Override
    public  R<Object> login(User user) {
        R<Object> resp = new R<>();
        if(ObjectUtils.isEmpty(user)){
            resp.setMsg("数据接收异常!");
            return resp;
        }
        String username = user.getUsername();
        String password = user.getPassword();
        User user1 = selectUserByUsername(username);
        //用户是否存在
        if(ObjectUtils.isEmpty(user1)) {
            resp.setMsg("您输入的账号不存在，请重新输入");
            return resp;
        }

        String password1 = user1.getPassword();
        //判断密码是否正确
        if (!"".equals(password)&&password1.equals(password)){
            //登陆成功！
            resp.setMsg("欢迎您: "+ username);
            resp.setCode(1);
            resp.add("userInfo",user1);
            return resp;

        }else {
            resp.setMsg("您输入的密码不正确，请重新输入");
            return resp;
        }
    }

    @Override
    public R<Object> updatePassword(User user) {

        baseMapper.updateById(user);
        R<Object> resp = new R<>();
        resp.setMsg("重置密码成功!请重新登陆...");
        return resp;
    }

    @Override
    public R<Object> updateUserInfo(User user) {
        log.info("uid  "+user.getUid());
        baseMapper.updateById(user);
        R<Object> resp = new R<>();
        resp.setMsg("重置用户信息成功...");
        return resp;
    }

    /**
     * 根据用户名查询用户
     * @param username
     */
    public User selectUserByUsername(String username) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getUsername,username);
            List<User> users = baseMapper.selectList(queryWrapper);
            if(!ObjectUtils.isEmpty(users)){
                return users.get(0);
            }else return null;
    }
}




